﻿using DTcms.Common;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;

namespace DTcms.Web.Mvc.UI
{
    public class WebApiController : ApiController
    {
        protected internal Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig();
        protected internal Model.userconfig userConfig = new BLL.userconfig().loadConfig();
        protected BaseController _base = new BaseController();
        protected Model.users userModel;

        protected ModelResponse<dynamic> mr = new ModelResponse<dynamic>();
        protected ModelResponsePage<dynamic> mrPage = new ModelResponsePage<dynamic>();
        protected int totalcount = 0;//总记录数
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            if (_base.IsUserLogin())
            {
                userModel = _base.GetUserInfo();
            }
        }

        #region 校检网站验证码
        public string verify_code(string strcode, string codeindex, bool remove = true)
        {
            if (string.IsNullOrEmpty(strcode) || string.IsNullOrEmpty(codeindex))
            {
                return "请点击对应字符激活！";
            }
            if (HttpContext.Current.Session[DTKeys.SESSION_CODE] == null)
            {
                return "验证码超时或已过期！";
            }
            string SESSION_CODE = HttpContext.Current.Session[DTKeys.SESSION_CODE].ToString();
            string[] code = Utils.SplitString(SESSION_CODE, ",");
            if (code[2] != codeindex || code[3] != strcode)
            {
                HttpContext.Current.Session[DTKeys.SESSION_CODE] = null;
                return "验证码与系统的不一致！";
            }
            if (remove)
            {
                HttpContext.Current.Session[DTKeys.SESSION_CODE] = null;
            }
            return "success";
        }

        public string verify_code(string strcode)
        {
            if (string.IsNullOrEmpty(strcode))
            {
                return "对不起，请输入验证码！";
            }
            if (HttpContext.Current.Session[DTKeys.SESSION_CODE] == null)
            {
                return "对不起，验证码超时或已过期！";
            }
            if (strcode.ToLower() != HttpContext.Current.Session[DTKeys.SESSION_CODE].ToString().ToLower())
            {
                return "您输入的验证码与系统的不一致！";
            }
            HttpContext.Current.Session[DTKeys.SESSION_CODE] = null;
            return "success";
        }
        #endregion
    }
}
